<?php
/**
 * 货品列表控制器
 */
class GoodsListController extends IsLoginController{
	private $Model;
	public function __init(){
		parent::__init();
		$this->Model = K('GoodsList');
	}	
	//货品列表首页
	public function index(){
		//获取商品id 查询关联表
		if(isset($_GET['gid']) && (int)$_GET['gid'] !== 0 ){
			$gid = Q('get.gid');
		}else{
			$this->error('不可访问路径',__CONTROLLER__);
		}
		//查询商品获取类型id
		$goodstData = K('goods')->where("gid = '{$gid}'")->find();
		$tid = $goodstData['type_tid'];
		//关联表 查询当前商品所对应的  所有规格属性
		$typeAttr = M()->JOIN("__type_attr__ type  JOIN __goods_attr__ gattr  ON  type.aid = gattr.type_attr_aid ")->where("type_tid ='{$tid}' AND attr_class=1 AND goods_gid = '{$gid}' ")->all(); 
		//查询当前商品下的货品列表信息
		$listData = $this->Model->where("goods_gid = '{$gid}'")->all();
		foreach($listData as $k =>$v){
			$attr = explode(',', $v['goods_list_compositeAttrId']); //分割字符串为数组
			foreach($attr as $ke =>$va){  //循环字符串分割后的值  判断值匹配
				//内层循环判断  跑查询到的商品属性表  判断匹配  
				foreach($typeAttr as $key => $val){
					if($va == $val['goods_attr_id']){ //匹配后吧 属性值追加到商品属性 货品数据中
						$listData[$k]['attrValue'][] = $val['goods_attr_value'];
						$listData[$k]['attrName'][] = $val['attr_name'];
					}
				}

			}
		}
		$this->assign('listData',$listData);
		
		$this->display();
	}
	
	//添加货品
	public function addGoodsList(){
		//获取商品id 查询关联表
		if(isset($_GET['gid']) && (int)$_GET['gid'] !== 0 ){
			$gid = Q('get.gid');
		}else{
			$this->error('不可访问路径',__CONTROLLER__);
		}
		
		$goodstData = K('goods')->where("gid = '{$gid}'")->find();
		$tid = $goodstData['type_tid'];
		if(IS_POST){ 
			if(!$this->Model->addGoodsList()){
				$this->error($this->Model->error);
			}
			$this->success('添加成功');				
		}
		//商品表goods  类型type_tid 关联 类型属性 type_attr  type_tid  关联商品属性表 goods_attr type_attr_aid 
		$typeAttr = M()->JOIN("__type_attr__ type  JOIN __goods_attr__ gattr  ON  type.aid = gattr.type_attr_aid ")->where("type_tid ='{$tid}' AND attr_class=1 AND goods_gid = '{$gid}' ")->all();
		//取出相同所有aid  
		foreach($typeAttr as $k => $v ){
			$typeAttrAid['aid'][] = $v['aid'];
		}
		//过滤重复aid
		$typeAttrAid= array_unique($typeAttrAid['aid']); 
		//重组数组 将相同aid 值放一起 为分配视图遍历提供方便
		foreach($typeAttr as $k => $v ){
			foreach($typeAttrAid as $key =>$val){
				if($v['aid'] == $val){
					$typeAttrData[$key]['goods_attr_value'][] = $v['goods_attr_value'];
					$typeAttrData[$key]['goods_attr_id'][] = $v['goods_attr_id'];
				}
			}
		}
		//分配视图细数据
		$this->assign('typeAttrData',$typeAttrData);
		$this->display();
	}

	//编辑货品单
	public function editGoodsList(){
		
		//获取商品id 
//		if(isset($_GET['gid']) && (int)$_GET['gid'] !== 0 ){
			$gid = Q('get.gid');
//		}else{
//			$this->error('不可访问路径',__CONTROLLER__);
//		}
		$lid = Q('get.lid');
		//提交表单处理事件
		if(IS_POST){ 
			if(!$this->Model->editGoodsList()){
				$this->error($this->Model->error);
			}
			$this->success('修改成功');				
		}
		//获取商品属性id 和规格属性id
		$goodstData = K('goods')->where("gid = '{$gid}'")->find();
		$tid = $goodstData['type_tid'];
				//商品表goods  类型type_tid 关联 类型属性 type_attr  type_tid  关联商品属性表 goods_attr type_attr_aid 
		$typeAttr = M()->JOIN("__type_attr__ type  JOIN __goods_attr__ gattr  ON  type.aid = gattr.type_attr_aid ")->where("type_tid ='{$tid}' AND attr_class=1 AND goods_gid = '{$gid}' ")->all();
		//取出相同所有aid  
		foreach($typeAttr as $k => $v ){
			$typeAttrAid['aid'][] = $v['aid'];
		}
		//过滤重复aid
		$typeAttrAid= array_unique($typeAttrAid['aid']); 
		//重组数组 将相同aid 值放一起 为分配视图遍历提供方便
		foreach($typeAttr as $k => $v ){
			foreach($typeAttrAid as $key =>$val){
				if($v['aid'] == $val){
					$typeAttrData[$key]['goods_attr_value'][] = $v['goods_attr_value'];
					$typeAttrData[$key]['goods_attr_id'][] = $v['goods_attr_id'];
				}
			}
		}
		//分配视图数据   规格选择项
		$this->assign('typeAttrData',$typeAttrData);
		
		
		//获取商品id 和规格属性id  组合数据提供判断 默认显示值
		$goodsData = $this->Model->where("goods_list_id = '{$lid}'")->find();
		//查询当前这条的数据库值  赋给视图页面
			$goodsData['attr'] = M('goods_attr')->where("goods_attr_id IN (" . $goodsData['goods_list_compositeAttrId'] . ")")->getField('goods_attr_value', true);//通过 商品组合附加属性 字段中 查询商品属性表中有  对应值
		//将配置当前选中状态的默认值分配到页面  
		$this->assign('goodsData',$goodsData);
		$this->display();
	}
	
	
	//删除货品表
	public function del(){
		$listId = Q('get.goods_list_id');
		$listInfo = $this->Model->where("goods_list_id = '{$listId}'")->del();
		$this->assign('listInfo',$listInfo);
		$this->success('删除成功');
	}
	
}
